#!/usr/bin/python
#coding=utf-8
'''
@author : steven feng
'''

import unittest
from PINFetcher import PINFetcher
from weibopy.auth import OAuthHandler
from weibopy.api import API

class SFSWeibo(unittest.TestCase):
    consumer_key = ""
    consumer_secret = ""
    fetcher = PINFetcher() 
    def __init__(self):
        token = open('Token.txt','r')
        self.consumer_key = token.readline().strip()
        self.consumer_secret = token.readline().strip()
        token.close() 
        print  self.consumer_key
        print  self.consumer_secret
    def getAtt(self, key):
        try:
            return self.obj.__getattribute__(key)
        except Exception, e:
            print e
            return ''
        
    def getAttValue(self, obj, key):
        try:
            return obj.__getattribute__(key)
        except Exception, e:
            print e
            return ''
        
    def auth(self):
        
        if len(self.consumer_key) == 0:
            print "Please set consumer_key"
            return
        
        if len(self.consumer_key) == 0:
            print "Please set consumer_secret"
            return
                
        self.auth = OAuthHandler(self.consumer_key, self.consumer_secret)
        auth_url = self.auth.get_authorization_url()
        print auth_url
        verifier = self.fetcher.getAccessCode(auth_url).strip()
        print verifier
        self.auth.get_access_token(verifier)
        self.api = API(self.auth)
  
    def setToken(self, token, tokenSecret):
        self.auth = OAuthHandler(self.consumer_key, self.consumer_secret)
        self.auth.setToken(token, tokenSecret)
        self.api = API(self.auth)
    
    def update(self, message):
        status = self.api.update_status(message)
        self.obj = status
        id = self.getAtt("id")
        text = self.getAtt("text")
        print "update---"+ str(id) +":"+ text
        
    def destroy_status(self, id):
        status = self.api.destroy_status(id)
        self.obj = status
        id = self.getAtt("id")
        text = self.getAtt("text")
        print "update---"+ str(id) +":"+ text
sWeibo = SFSWeibo()
sWeibo.auth()
